package com.astrocsr.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.astrocsr.domain.bean.TblRole;

@Repository("roleDAO")
public class RoleDAO extends BaseHelperDAO {

	public static final String ROLE_NAME = "roleName";
	public static final String ROLE_CODE = "roleCode";

	public List<TblRole> findByRoleName(Object roleName) throws Exception {
		return findByProperty(ROLE_NAME, roleName);
	}

	public List<TblRole> findByRoleCode(Object roleCode) throws Exception {
		return findByProperty(ROLE_CODE, roleCode);
	}

	@SuppressWarnings("unchecked")
	public List<TblRole> findByProperty(String propertyName, Object value)
			throws Exception {

		try {

			String queryString = "from TblRole as model where model."
					+ propertyName + "= :propertyName";
			Query query = getCurrentSession().createQuery(queryString);
			query.setParameter("propertyName", value);
			return query.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<TblRole> listTblRole() throws Exception {
		List<TblRole> result = null;

		String queryString = "from TblRole"; // Hibernate Query Language
		result = getCurrentSession().createQuery(queryString).list();

		return result;
	}

	/**
	 * Insert customer into database
	 */
	public void addTblRole(TblRole entity) throws Exception {
		// Insert into database
		getCurrentSession().save(entity);
	}
}